<?php
namespace Activity\Model;
use Think\Model;
class ActivityMemberModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_activity_member'; 
	
	public function getMemberList($data){
		$map = array();
		
		if(!empty($data['activityid'])){
			$map['activityid']  = $data['activityid'];
		}
		
		if(!empty($data['custid'])){
			$map['custid']  = $data['custid'];
		}
		
		if($data['statusFilter']==="1" || $data['statusFilter']==="0")
			$map['status'] = $data['statusFilter'];
		
		$total = $this->where($map)->count();
		$fields = array('id','activityid','custid','status','checkintime','createtime','updatetime');
		if(empty($data['sortField']))$data['sortField'] = "id";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
		
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else 
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
		
		
		return array('total'=>$total , 'data'=>$result);
	}
	
	
	public function countMember($activityid){
		$map['activityid'] = $activityid;
		$ret = $this->where($map)->count();
		return $ret;
	}
	
	public function checkMember($data){
		$map['activityid'] = $data['activityid'];
		$map['custid'] = $data['custid'];
		$ret = $this->where($map)->find();
		if(!empty($ret)){
			return 1;
		}else{
			return 0;
		}
	}
	
	public function addMember($data){
		$map['activityid'] = $data['activityid'];
		$map['custid'] = $data['custid'];
		$ret = $this->where($map)->find();
		if(!empty($ret)){
			return $ret['id'];
		}else{
			$result = $this->add($data);
			return $result;
		}
	}
	
	public function updateMember($data){
		$map['activityid'] = $data['activityid'];
		$map['custid'] = $data['custid'];
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	public function deleteMember($data){
		$map['activityid'] = $data['activityid'];
		$map['custid'] = $data['custid'];
		$result = $this->where($map)->delete();
		return $result;
	}
}


